Kernel.module.define('media', {
	
	title: 'media',
	file: 'media.php',
	widget: true,
	
	init: function() {
		var module = this;
		var y_height = window.innerHeight;

		$("#dashNowplayingLyr").animate({opacity: 0, top: (y_height+100)+"px"}, 1000, function() {});

		//localStorage.setItem("CONFIG_MEDIA_DIRECTORY", "http://media.localhost.com/");
		var smv = localStorage.getItem("CONFIG_STARTUP_VOLUME");
		if (parseInt(smv) < 0 || parseInt(smv) > 100) {
			smv = 50;
		}
		
		var muteIcon = "fa fa-bell-o fa-2x";
		$("#mpMute").html("<i class='"+muteIcon+"'></i>");

		/*******************************************************
		 * Events
		 ******************************************************/

		$("#coverart").on("click", function() {
			module.sandbox.putmRunningFSA("#fsa-mediaplayer");
			module.sandbox.putmRunningTitle("mediaplayer");
			$("#fsa-mediaplayer").slideDown(500);
		});
		
		$("#mpMute").on("click", function() {
			if (module.sandbox.audioIsMute()) {
				var muteIcon = "fa fa-bell-o fa-2x";
			} else {
				var muteIcon = "fa fa-bell-slash-o fa-2x";
			}
			module.sandbox.audioToggleMute();
			$("#mpMute").html("<i class='"+muteIcon+"'></i>");
		});
		
		$("#mpPrev").on("click", function() {
			var prevTrack = $("li.highlight").prev();

			if (prevTrack.length > 0) {
				var liContent = prevTrack.html().split("<p>");
				var art = liContent[0];
				module.sandbox.audioPlay(prevTrack.attr('id'), smv, art, prevTrack.attr('data-id3title'), prevTrack.attr('data-id3album'), prevTrack.attr('data-id3artist'), prevTrack.attr('data-id3genre'));
				$("li.highlight").removeClass("highlight");
				prevTrack.addClass("highlight");
				hl = prevTrack;
			}
		});

		$("#mpStop").on("click", function() {
			module.sandbox.audioStop();
		});
		
		$("#mpNext").on("click", function() {
			var nextTrack = $("li.highlight").next();

			if (nextTrack.length > 0) {
				var liContent = nextTrack.html().split("<p>");
				var art = liContent[0];
				module.sandbox.audioPlay(nextTrack.attr('id'), smv, art, nextTrack.attr('data-id3title'), nextTrack.attr('data-id3album'), nextTrack.attr('data-id3artist'), nextTrack.attr('data-id3genre'));
				$("li.highlight").removeClass("highlight");
				nextTrack.addClass("highlight");
				hl = nextTrack;
			}
		});
		
		$("#dashNowplayingLyr, #mediaContents").on("selectstart dragstart", function(e) {
			e.preventDefault();
			e.stopPropagation();
			return false;
		});

		module.sandbox.listen('audio started', function(data) {
			//module.drawVisual();
			$("#coverart").html(module.sandbox.audioPlayingArt);
			
			if (data.length < 40) {
				$("#npTitle").html(data);
			} else {
				$("#npTitle").html(data.substr(0, 37)+"...");
			}
			$("#dashNowplayingLyr").show();
			$("#dashNowplayingLyr").animate({opacity: 1, top: (window.innerHeight-100)+"px"}, 1000, function() {});
		});

		module.sandbox.listen('audio end', function(data) {
			$("#npTitle").html("Change to weather");
			$("#dashNowplayingLyr").animate({opacity: 0, top: "600px"}, 1000, function() {$("#dashNowplayingLyr").hide();});
		});
	},
	
	drawVisual: function() {
		var module = this;
		var avCanvas = document.getElementById("avCanvas");
		var avCtx = avCanvas.getContext('2d');
		
		avCanvas.width = 100;
		avCanvas.height = 100;
		avCtx.fillStyle = '#00cc00';		

		var animReq = requestAnimationFrame(module.drawVisual);
		// Create a new array that we can copy the frequency data into
		var freqByteData = new Uint8Array(analyser.frequencyBinCount);
		// Copy the frequency data into our new array
		analyser.getByteFrequencyData(freqByteData);

		// Clear the drawing display
		avCtx.clearRect(0, 0, avCanvas.width, avCanvas.height);

		// For each "bucket" in the frequency data, draw a line corresponding to its magnitude
		for (var i = 0; i < freqByteData.length; i=i+8) {
			var aFreqPercent = freqByteData[i]/256;
			if (aFreqPercent < 100) {
				var lheight = (avCanvas.height * aFreqPercent) - 1;
			} else {
				var lheight = 100;
			}
			
			if (lheight > 75) {
				avCtx.fillStyle = '#00cc00';		
			} else if (lheight > 25) {
				avCtx.fillStyle = '#00cc00';		
			} else {
				avCtx.fillStyle = '#00aa00';		
			}
			avCtx.fillRect(i, avCanvas.height-lheight, 5, avCanvas.height);
		}			
		
		if (!module.sandbox.audioIsPlaying()) {
			cancelAnimationFrame(animReq);
		}
	}
});
